//
//  HomeTableViewCourseCell.m
//  268EDU_Demo
//
//  Created by 刘星 on 2017/9/12.
//  Copyright © 2017年 edu268. All rights reserved.
//

#import "HomeTableViewCourseCell.h"
#import "HomeListModel.h"
#import "ServiceLiveModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface HomeTableViewCourseCell ()

@property (weak, nonatomic) IBOutlet UIImageView *exhibitImageView;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *playImageView;
@property (weak, nonatomic) IBOutlet UILabel *tipMessageLabel;
@property (weak, nonatomic) IBOutlet UILabel *listTilteLabel;
@property (weak, nonatomic) IBOutlet UILabel *listContentLabel;
@property (weak, nonatomic) IBOutlet UIView *verticalLineView;
@property (weak, nonatomic) IBOutlet UIImageView *watchImageView;
@property (weak, nonatomic) IBOutlet UILabel *watchNumlabel;
@property (weak, nonatomic) IBOutlet UIImageView *praseImageView;
@property (weak, nonatomic) IBOutlet UILabel *praseNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation HomeTableViewCourseCell

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.avatarImageView.layer.cornerRadius = 13;
    self.avatarImageView.layer.masksToBounds = YES;
}

- (UIImage *)loadImageViewWitName:(NSString *)name { // @"tabbar_bg@3x.png"
    NSBundle *currentBundle = [NSBundle bundleForClass:[self class]];
    NSString *bundleName = [currentBundle.infoDictionary[@"CFBundleName"] stringByAppendingString:@".bundle"];
    NSString *path = [currentBundle pathForResource:name ofType:nil inDirectory:bundleName];
    NSLog(@"%@", path);
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}

- (void)setListModel:(HomeListModel *)listModel {
    _listModel = listModel;
    UIImage *loadImageViewdefaultImage = [self loadImageViewWitName:@"servcie_avtar_default@2x.png"];
    if (_listModel.type == HomeListTypeCourse || _listModel.type == HomeListTypePackage) { // 课程 或者 套餐
        self.avatarImageView.hidden = YES;
        self.nameLabel.hidden = YES;
        self.playImageView.hidden = YES;
        self.tipMessageLabel.hidden = YES;
        self.dayLabel.hidden = YES;
        self.timeLabel.hidden = YES;
        self.verticalLineView.hidden = NO;
        [self.exhibitImageView sd_setImageWithURL:[NSURL URLWithString:_listModel.image] placeholderImage:loadImageViewdefaultImage];
        self.listTilteLabel.text = _listModel.name;
        self.listContentLabel.text = _listModel.content;
        if (_listModel.browse_counts > 10000) {
            self.watchNumlabel.text = @"9999+";
        } else {
            self.watchNumlabel.text = [NSString stringWithFormat:@"%ld", _listModel.browse_counts];
        }
        if (_listModel.praise_counts > 10000) {
            self.praseNumLabel.text = @"9999+";
        } else {
            self.praseNumLabel.text = [NSString stringWithFormat:@"%ld", _listModel.praise_counts];
        }
    } else if (_listModel.type == HomeListTypeLive) { // 直播
        self.avatarImageView.hidden = YES;
        self.nameLabel.hidden = YES;
        self.playImageView.hidden = NO;
        self.tipMessageLabel.hidden = NO;
        self.dayLabel.hidden = YES;
        self.timeLabel.hidden = YES;
        if (_listModel.livePlayStaut == ServiceCourseLiveForecast) {
            self.dayLabel.hidden = NO;
            self.timeLabel.hidden = NO;
            self.tipMessageLabel.textColor = [UIColor whiteColor];
            self.tipMessageLabel.text = @"即将直播";
            self.playImageView.image = [self loadImageViewWitName:@"service_played@2x.png"];
            self.verticalLineView.hidden = YES;
            [self.exhibitImageView sd_setImageWithURL:[NSURL URLWithString:_listModel.image] placeholderImage:loadImageViewdefaultImage];
            self.dayLabel.text = [_listModel.recommend_time substringWithRange:NSMakeRange(0, 10)];
            self.timeLabel.text = [_listModel.recommend_time substringWithRange:NSMakeRange(10, _listModel.recommend_time.length - 10)];
            self.listTilteLabel.text = _listModel.name;
            self.listContentLabel.text = _listModel.content;
        } else if (_listModel.livePlayStaut == ServiceCourseLiveOn) {
            self.verticalLineView.hidden = NO;
            self.dayLabel.hidden = YES;
            self.dayLabel.hidden = YES;
            self.tipMessageLabel.textColor = UIColorFromRGB(0xfe4070);
            self.tipMessageLabel.text = @"正在直播...";
            self.playImageView.image = [self loadImageViewWitName:@"service_playing@2x.png"];
            self.listTilteLabel.text = _listModel.name;
            self.listContentLabel.text = _listModel.content;
            [self.exhibitImageView sd_setImageWithURL:[NSURL URLWithString:_listModel.image] placeholderImage:loadImageViewdefaultImage];
            if (_listModel.browse_counts > 10000) {
                self.watchNumlabel.text = @"9999+";
            } else {
                self.watchNumlabel.text = [NSString stringWithFormat:@"%ld", _listModel.browse_counts];
            }
            if (_listModel.praise_counts > 10000) {
                self.praseNumLabel.text = @"9999+";
            } else {
                self.praseNumLabel.text = [NSString stringWithFormat:@"%ld", _listModel.praise_counts];
            }
        } else if (_listModel.livePlayStaut == ServiceCourseLiveReplay) {
            self.verticalLineView.hidden = NO;
            self.dayLabel.hidden = YES;
            self.dayLabel.hidden = YES;
            self.tipMessageLabel.textColor = [UIColor whiteColor];
            self.tipMessageLabel.text = @"直播回放";
            self.playImageView.image = [self loadImageViewWitName:@"service_playing@2x.png"];
            self.listTilteLabel.text = _listModel.name;
            self.listContentLabel.text = _listModel.content;
            [self.exhibitImageView sd_setImageWithURL:[NSURL URLWithString:_listModel.image] placeholderImage:loadImageViewdefaultImage];
            if (_listModel.browse_counts > 10000) {
                self.watchNumlabel.text = @"9999+";
            } else {
                self.watchNumlabel.text = [NSString stringWithFormat:@"%ld", _listModel.browse_counts];
            }
            if (_listModel.praise_counts > 10000) {
                self.praseNumLabel.text = @"9999+";
            } else {
                self.praseNumLabel.text = [NSString stringWithFormat:@"%ld", _listModel.praise_counts];
            }
        }
    } else if (_listModel.type == HomeListTypeCommunity) {
        self.avatarImageView.hidden = NO;
        self.nameLabel.hidden = NO;
        self.playImageView.hidden = YES;
        self.tipMessageLabel.hidden = YES;
        self.dayLabel.hidden = YES;
        self.timeLabel.hidden = YES;
        self.verticalLineView.hidden = NO;
        self.listTilteLabel.text = _listModel.name;
        self.listContentLabel.text =  [[self getZZwithString:_listModel.content] stringByReplacingOccurrencesOfString:@" " withString:@""];
        [self.exhibitImageView sd_setImageWithURL:[NSURL URLWithString:_listModel.image] placeholderImage:loadImageViewdefaultImage];
        if (_listModel.browse_counts > 10000) {
            self.watchNumlabel.text = @"9999+";
        } else {
            self.watchNumlabel.text = [NSString stringWithFormat:@"%ld", _listModel.browse_counts];
        }
        if (_listModel.praise_counts > 10000) {
            self.praseNumLabel.text = @"9999+";
        } else {
            self.praseNumLabel.text = [NSString stringWithFormat:@"%ld", _listModel.praise_counts];
        }
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:_listModel.avatar] placeholderImage:loadImageViewdefaultImage];
        self.nameLabel.text = _listModel.login_account;
    }
}

// 正则去除网络标签
- (NSString *)getZZwithString:(NSString *)string {
    NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n|&nbsp"
                                                                                    options:0
                                                                                      error:nil];
    string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
    return string;
}


@end
